<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace PHPCan\Cache;

defined('ANS') or die();

class Memcached extends \Memcached implements iCache
{
    private $Debug;
    private $connected;
    private $settings;

    /**
     * public function __construct ([string $autoglobal], [array $settings])
     *
     * return none
     */
    public function __construct ($autoglobal = '', $settings = array())
    {
        global $Debug;

        $this->Debug = $Debug;
        $this->settings = $settings;

        if ($autoglobal) {
            global $Config;

            $Config->config['autoglobal'][] = $autoglobal;
        }
    }

    /**
    * public function connect (string $type)
    *
    * Connect to memcached server
    *
    * return boolean
    */
    public function connect ($type)
    {
        if (!extension_loaded('memcached')) {
            $this->connected = false;

            return false;
        }

        parent::__construct();

        if ($Config->cache['memcached']) {
            $this->connected = $this->addServer($Config->cache['memcached']['host'], $Config->cache['memcached']['port']);
        } else {
            $this->connected = $this->addServer('localhost', 11211);
        }

        $this->setOption(Memcached::OPT_COMPRESSION, true);

        return $this;
    }

    /**
    * public function exists ($key)
    *
    * Check variable exists in memory
    *
    * return boolean
    */
    public function exists ($key)
    {
        if (!$this->connected) {
            return false;
        }

        if (parent::add($key, null)) {
            parent::delete($key);

            return false;
        } else {
            return true;
        }
    }

    /**
    * public function set ($key, $value, [$expire integer])
    *
    * Set a variable into memory
    *
    * return mixed
    */
    public function set ($key, $value, $expire = 3600)
    {
        if ($this->connected) {
            parent::set($key, $value, $expire);
        }

        return $value;
    }

    /**
    * public function get ($key)
    *
    * Get a variable from memory
    *
    * return mixed
    */
    public function get ($key)
    {
        return ($this->connected) ? parent::get($key) : null;
    }

    /**
    * public function delete ($key)
    *
    * Delete a variable from memory
    *
    * return mixed
    */
    public function delete ($key)
    {
        return ($this->connected) ? parent::delete($key) : null;
    }

    /**
    * public function clear (void)
    *
    * Clear APC cache
    *
    * return mixed
    */
    public function clear ()
    {
        return ($this->connected) ? parent::flush() : null;
    }

    /**
    * public function expire ($key)
    *
    * Return the time to expire a key
    *
    * return mixed
    */
    public function expire ($key)
    {
        if (!$this->loaded) {
            return null;
        }

        return false;
    }
}
